package com.borrowed.book.system.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DayUtil {
    /**
     * 计算两个日期之间相差的天数
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate)
    {
        Long between_days=0l;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            smdate = sdf.parse(sdf.format(smdate));
            bdate = sdf.parse(sdf.format(bdate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(smdate);
            long time1 = cal.getTimeInMillis();
            cal.setTime(bdate);
            long time2 = cal.getTimeInMillis();
            between_days = (time2 - time1) / (1000 * 3600 * 24);
        }catch (Exception e){
            e.printStackTrace();
        }

        return Integer.parseInt(String.valueOf(between_days));
    }


    /**
     * 获取某天最开始时刻
     * @param calendar
     * @return
     */
    public static Calendar setStartHourMinite(Calendar calendar){
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar;
    }

    /**
     * 获取某天最后一个时刻
     * @param calendar
     * @return
     */
    public static Calendar setLastHourMinite(Calendar calendar){
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar;
    }

    /**
     * 判断时间是否在范围之内
     * @param now  比对的时间
     * @param starDate 范围开始时间
     * @param endDate 范围结束时间
     * @return
     */
    public static boolean timeBetweenCapare(Date now,Date starDate,Date endDate){
        if(starDate==null||endDate==null||now==null){
            return false;
        }
        if(now.after(starDate)&&now.before(endDate)){
            return true;
        }else{
            return false;
        }
    }
}
